﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Diagnostics;
using ServiceStack.Text;
using ServiceStack;

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace EquipmentManager
{
    class Program
    {
        static ILog log;

        
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            ServiceStack.Licensing.RegisterLicense(@"3275-e1JlZjozMjc1LE5hbWU66JGb5rSy5Z2d6ZuG5Zui6K+V6aqM5qOA5rWL5pyJ6ZmQ5YWs5Y+4LFR5cGU6SW5kaWUsSGFzaDpuakRvUlpCdXYyNWZQTjg0YkNSTDcwaUVGaTFUUlFrTkxXaHpOTzFCdmJDVUZvZEFncm1vZytxelpZSHhrMllGQjdZcWhqZWtBQTdlRjlBblo4dEVhVjk3Z1ZxK2NXMmxZdjNoNFJ1U2xDU04rY0FpbnAyVUNLdE0wVFYwY0QyY2E1eXJRUGJaYVV2a1FvOGpHNEN3eTV1dldLRmtPTk91S2dDcEU2U3VxNDQ9LEV4cGlyeToyMDE2LTEyLTMxfQ==");
            log = LogManager.GetLogger("test_log");
            log.Info("程序启动！");
            GlobalConfig.CreateDataBase();


            //dongle = new WirelessDongle433M(Properties.Settings.Default.UseSimulator);
            //sensors = new CurrentSensors();
            //sensors.LoadSensors();
            //Timer timer = new Timer(sensors.Collect, null, 0, Settings.Default.PeriodicTime * 1000); //采样间隔

            try
            {
                //启动服务
                new AppHost().Init().Start(Properties.Settings.Default.WebServicePort);
                //启动log记录
                string startinfo = "http服务在 {0} 上启动".Fmt(Properties.Settings.Default.WebServicePort);
                startinfo.Print();
                log.Info(startinfo);
            }
            catch (Exception e)
            {
                "启动Web Service失败了".Print();
                e.ToString().Print();
                return;
            }
            if(Properties.Settings.Default.OpenBroswer)
                Process.Start("http://localhost:8010/index.html");
            string readline;
            do
            {
                Console.WriteLine("键入Quit并回车退出程序！");
                readline = Console.ReadLine();
            } while (readline != "Quit");
            log.Info("程序退出！");
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            log.ErrorFormat("应用程序未处理错误！ 错误信息如下：\r\n {0}", e.ExceptionObject.ToString());
        }
    }
}
